package com.bestcem.xm.user.grpc.service.converter;

import com.alibaba.fastjson.JSON;
import com.bestcem.bp.xm.user.grpc.v1.enums.OrgDeliveryAuthEnum;
import com.bestcem.bp.xm.user.grpc.v1.enums.OrgFooterEnum;
import com.bestcem.bp.xm.user.grpc.v1.enums.OrgSourceEnum;
import com.bestcem.bp.xm.user.grpc.v1.enums.OrgStatusEnum;
import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.enums.UserEnum;
import com.bestcem.xm.user.grpc.service.param.ListOrgsByFilterParam;
import com.bestcem.xm.user.grpc.service.param.PageParam;
import com.bestcem.xm.user.service.dto.user.OrgSearchDTO;
import com.bestcem.xm.user.service.dto.user.OrganizationDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * grpc请求出入参转化类
 *
 * @author jishuo.yan <jishuo.yan@idiaoyan.com>
 * @date 2022/3/8 16:29
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrganizationConvert {

    /**
     * 将本地处理结果转换成grpc请求响应参数：OrganizationDTO转为Organization
     *
     * @param dto
     * @return
     * @author yanjishuo <jishuo.yan@idiaoyan.com>
     * @date 2022/03/08 15:46
     */
    public static Organization organizationDtoToOrganization(OrganizationDTO dto) {
        Organization.Builder builder = Organization.newBuilder();

        if (StringUtils.isNotBlank(dto.getOrgId())) {
            builder.setId(dto.getOrgId());
        }
        if (StringUtils.isNotBlank(dto.getCode())) {
            builder.setCode(dto.getCode());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            builder.setName(dto.getName());
        }
        if (Objects.nonNull(dto.getStatus())) {
            builder.setStatus(OrgStatusEnum.OrgStatus.forNumber(dto.getStatus()));
        }
        if (Objects.nonNull(dto.getSource())) {
            builder.setSource(OrgSourceEnum.OrgSource.forNumber(dto.getSource()));
        }
        if (StringUtils.isNotBlank(dto.getRootGroupId())) {
            builder.setRootGroupId(dto.getRootGroupId());
        }
        if (StringUtils.isNotBlank(dto.getIndustryId())) {
            builder.setIndustryId(dto.getIndustryId());
        }
        if (StringUtils.isNotBlank(dto.getPackageId())) {
            builder.setPackageId(dto.getPackageId());
        }
        if (StringUtils.isNotBlank(dto.getFormalPackageId())) {
            builder.setFormalPackageId(dto.getFormalPackageId());
        }
        if (Objects.nonNull(dto.getFormalExpireTime())) {
            builder.setFormalExpireTime(GrpcDateUtil.getTimestamp(dto.getFormalExpireTime()));
        }
        if (StringUtils.isNotBlank(dto.getTrialPackageId())) {
            builder.setTrialPackageId(dto.getTrialPackageId());
        }
        if (Objects.nonNull(dto.getTrialExpireTime())) {
            builder.setTrialExpireTime(GrpcDateUtil.getTimestamp(dto.getTrialExpireTime()));
        }
        if (StringUtils.isNotBlank(dto.getAdminEmail())) {
            builder.setAdminEmail(dto.getAdminEmail());
        }
        if (StringUtils.isNotBlank(dto.getAdminPhone())) {
            builder.setAdminPhone(dto.getAdminPhone());
        }
        if (Objects.nonNull(dto.getPackageExpireTime())) {
            builder.setPackageExpireTime(GrpcDateUtil.getTimestamp(dto.getPackageExpireTime()));
        }
        if (Objects.nonNull(dto.getFreeExpireTime())) {
            builder.setFreeExpireTime(GrpcDateUtil.getTimestamp(dto.getFreeExpireTime()));
        }
        if (StringUtils.isNotBlank(dto.getPrivateKeyId())) {
            builder.setPriId(dto.getPrivateKeyId());
        }
        if (Objects.nonNull(dto.getDeliverCheckFlag())) {
            builder.setDeliverAudit(dto.getDeliverCheckFlag().equals(UserEnum.USER_YES.getFlag()) ? Boolean.TRUE : Boolean.FALSE);
        }
        if (Objects.nonNull(dto.getCompleteFlag())) {
            builder.setComplete(dto.getCompleteFlag().equals(UserEnum.USER_YES.getFlag()) ? Boolean.TRUE : Boolean.FALSE);
        }
        if (StringUtils.isNotBlank(dto.getAdminName())) {
            builder.setAdminName(dto.getAdminName());
        }
        if (StringUtils.isNotBlank(dto.getCorpWallet())) {
            builder.setWalletId(dto.getCorpWallet());
        }
        if (Objects.nonNull(dto.getSmsPrice())) {
            builder.setSmsPrice(dto.getSmsPrice());
        }
        if (Objects.nonNull(dto.getFooter())) {
            builder.setFooter(OrgFooterEnum.OrgFooter.forNumber(dto.getFooter()));
        }
        if (StringUtils.isNotBlank(dto.getCheckSmtpId())) {
            builder.setAuditSmtpId(dto.getCheckSmtpId());
        }
        if (Objects.nonNull(dto.getInnerFlag())) {
            builder.setInner(dto.getInnerFlag().equals(UserEnum.USER_YES.getFlag()) ? Boolean.TRUE : Boolean.FALSE);
        }
        if (Objects.nonNull(dto.getDeliveryAuthType())) {
            builder.setDeliveryAuth(OrgDeliveryAuthEnum.OrgDeliveryAuth.forNumber(dto.getDeliveryAuthType()));
        }
        if (Objects.nonNull(dto.getCreateTime())) {
            builder.setCreateTime(GrpcDateUtil.getTimestamp(dto.getCreateTime()));
        }
        if (StringUtils.isNotBlank(dto.getPaasConf())) {
            builder.setPaasConf(dto.getPaasConf());
        }
        if (StringUtils.isNotBlank(dto.getContent())) {
            builder.setContent(dto.getContent());
        }
        if (StringUtils.isNotBlank(dto.getDistribution())) {
            builder.setDistribution(dto.getDistribution());
        }
        if (StringUtils.isNotBlank(dto.getContractUrls())) {
            builder.addContractUrls(dto.getContractUrls());
        }
        if (StringUtils.isNotBlank(dto.getSkin())) {
            builder.setSkin(dto.getSkin());
        }
        if (StringUtils.isNotBlank(dto.getSourceRemark())) {
            builder.setSourceRemark(dto.getSourceRemark());
        }
        if (StringUtils.isNotBlank(dto.getPublicKeyFilePath())) {
            builder.setPubFilePath(dto.getPublicKeyFilePath());
        }
        if (StringUtils.isNotBlank(dto.getParentCode())) {
            builder.setParentCode(dto.getParentCode());
        }
        if (StringUtils.isNotBlank(dto.getParentId())) {
            builder.setParentId(dto.getParentId());
        }
        if (Objects.nonNull(dto.getBanLoginTrialExpiration())) {
            builder.setBanLoginTrialExpiration(dto.getBanLoginTrialExpiration().equals(UserEnum.USER_YES.getFlag()) ? Boolean.TRUE : Boolean.FALSE);
        }
        if (Objects.nonNull(dto.getSensitiveWordEnable())) {
            builder.setSensitiveWordEnable(dto.getSensitiveWordEnable().equals(UserEnum.USER_YES.getFlag()) ? Boolean.TRUE : Boolean.FALSE);
        }
        return builder.build();
    }

    /**
     * 将本地处理结果转换成grpc请求响应参数：OrganizationDTO转为Organization
     *
     * @param dtos
     * @return
     * @author yanjishuo <jishuo.yan@idiaoyan.com>
     * @date 2022/03/08 15:46
     */
    public static List<Organization> organizationDtosToOrganizations(List<OrganizationDTO> dtos) {
        List<Organization> organizationList = new ArrayList<>();
        for (OrganizationDTO dto : dtos) {
            Organization organization = organizationDtoToOrganization(dto);
            organizationList.add(organization);
        }
        return organizationList;
    }

    /**
     * grpc请求入参转换成本地入参：OrgSearch转为OrgSearchDTO
     *
     * @param orgSearch
     * @return orgSearchDTO
     * @author yanjishuo <jishuo.yan@idiaoyan.com>
     * @date 2022/03/16 17:20
     */
    public static OrgSearchDTO orgSearchToOrgsearchDTO(OrgSearch orgSearch) {
        OrgSearchDTO dto = new OrgSearchDTO();
        dto.setCode(orgSearch.getCode());
        dto.setName(orgSearch.getName());
        return dto;
    }

    public static OrganizationDTO organization2OrganizationDTO(Organization organization) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setOrgId(organization.getId());
        organizationDTO.setCode(organization.getCode());
        organizationDTO.setName(organization.getName());
        organizationDTO.setContent(organization.getContent());
        organizationDTO.setStatus(organization.getStatus().getNumber());
        //枚举类转换
        organizationDTO.setSource(organization.getSource().getNumber());
        organizationDTO.setSourceRemark(organization.getSourceRemark());
        organizationDTO.setIndustryId(organization.getIndustryId());
        organizationDTO.setPackageId(organization.getPackageId());
        organizationDTO.setFormalPackageId(organization.getFormalPackageId());
        //时间类型转换
        if (organization.hasFormalExpireTime()) {
            organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(organization.getFormalExpireTime()));
        }
        organizationDTO.setTrialPackageId(organization.getTrialPackageId());
        if (organization.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(organization.getTrialExpireTime()));
        }
        organizationDTO.setAdminEmail(organization.getAdminEmail());
        organizationDTO.setAdminPhone(organization.getAdminPhone());
        if (organization.hasPackageExpireTime()) {
            organizationDTO.setPackageExpireTime(GrpcDateUtil.getDate(organization.getPackageExpireTime()));
        }
        if (organization.hasFreeExpireTime()) {
            organizationDTO.setFreeExpireTime(GrpcDateUtil.getDate(organization.getFreeExpireTime()));
        }
        organizationDTO.setSkin(organization.getSkin());
        organizationDTO.setPublicKeyFilePath(organization.getPubFilePath());
        organizationDTO.setPrivateKeyId(organization.getPriId());
        organizationDTO.setDeliverCheckFlag(organization.getDeliverAudit() ? 1 : 0);
        organizationDTO.setCompleteFlag(organization.getComplete() ? 1 : 0);
        organizationDTO.setAdminName(organization.getAdminName());
        organizationDTO.setCorpWallet(organization.getWalletId());
        organizationDTO.setSmsPrice((short) organization.getSmsPrice());
        organizationDTO.setFooter(organization.getFooter().getNumber());
        organizationDTO.setCheckSmtpId(organization.getAuditSmtpId());
        organizationDTO.setNotifySalesFlag(organization.getNotifySales() ? 1 : 0);
        organizationDTO.setInnerFlag(organization.getInner() ? 1 : 0);
        organizationDTO.setPaasConf(organization.getPaasConf());
        organizationDTO.setDistribution(organization.getDistribution());
        if (CollectionUtils.isNotEmpty(organization.getContractUrlsList())) {
            organizationDTO.setContractUrls(JSON.toJSONString(organization.getContractUrlsList()));
        }
        organizationDTO.setDeliveryAuthType(organization.getDeliveryAuthValue());
        return organizationDTO;
    }

    public static OrganizationDTO orgCondition2OrganizationDTO(OrgCondition organization) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setOrgId(organization.getId());
        organizationDTO.setCode(organization.getCode());
        organizationDTO.setName(organization.getName());
        organizationDTO.setContent(organization.getContent());
        organizationDTO.setStatus(organization.hasStatus() ? organization.getStatusValue() : null);
        //枚举类转换
        organizationDTO.setSource(organization.hasSource() ? organization.getSourceValue() : null);
        organizationDTO.setSourceRemark(organization.getSourceRemark());
        organizationDTO.setIndustryId(organization.getIndustryId());
        organizationDTO.setPackageId(organization.getPackageId());
        organizationDTO.setFormalPackageId(organization.getFormalPackageId());
        //时间类型转换
        if (organization.hasFormalExpireTime()) {
            organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(organization.getFormalExpireTime()));
        }
        organizationDTO.setTrialPackageId(organization.getTrialPackageId());
        if (organization.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(organization.getTrialExpireTime()));
        }
        organizationDTO.setAdminEmail(organization.getAdminEmail());
        organizationDTO.setAdminPhone(organization.getAdminPhone());
        if (organization.hasPackageExpireTime()) {
            organizationDTO.setPackageExpireTime(GrpcDateUtil.getDate(organization.getPackageExpireTime()));
        }
        if (organization.hasFreeExpireTime()) {
            organizationDTO.setFreeExpireTime(GrpcDateUtil.getDate(organization.getFreeExpireTime()));
        }
        organizationDTO.setSkin(organization.getSkin());
        organizationDTO.setPublicKeyFilePath(organization.getPubFilePath());
        organizationDTO.setPrivateKeyId(organization.getPriId());
        if (organization.hasDeliverAudit()) {
            organizationDTO.setDeliverCheckFlag(organization.getDeliverAudit() ? 1 : 0);
        }
        if (organization.hasComplete()) {
            organizationDTO.setCompleteFlag(organization.getComplete() ? 1 : 0);
        }
        organizationDTO.setAdminName(organization.getAdminName());
        organizationDTO.setCorpWallet(organization.getWalletId());
        organizationDTO.setSmsPrice(organization.hasSmsPrice() ? ((short) organization.getSmsPrice()) : null);
        organizationDTO.setFooter(organization.hasFooter() ? organization.getFooterValue() : null);
        organizationDTO.setCheckSmtpId(organization.getAuditSmtpId());
        if (organization.hasNotifySales()) {
            organizationDTO.setNotifySalesFlag(organization.getNotifySales() ? 1 : 0);
        }
        if (organization.hasInner()) {
            organizationDTO.setInnerFlag(organization.getInner() ? 1 : 0);
        }
        organizationDTO.setPaasConf(organization.getPaasConf());
        organizationDTO.setDistribution(organization.getDistribution());
        if (CollectionUtils.isNotEmpty(organization.getContractUrlsList())) {
            organizationDTO.setContractUrls(organization.getContractUrls(0));
        }
        organizationDTO.setDeliveryAuthType(organization.hasDeliveryAuth() ? organization.getDeliveryAuthValue() : null);
        return organizationDTO;
    }

    /**
     * 公司操作信息转换成公司信息DTO对象
     *
     * @param org 公司操作信息
     * @return com.bestcem.xm.user.service.dto.user.OrganizationDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/30 10:40
     */
    public static OrganizationDTO operateOrgToOrganizationDTO(OperateOrg org) {
        OrganizationDTO organizationDTO = new OrganizationDTO();

        if (StringUtils.isNotBlank(org.getCode())) {
            organizationDTO.setCode(org.getCode());
        }
        if (StringUtils.isNotBlank(org.getName())) {
            organizationDTO.setName(org.getName());
        }
        if (StringUtils.isNotBlank(org.getAdminEmail())) {
            organizationDTO.setAdminEmail(org.getAdminEmail());
        }
        if (StringUtils.isNotBlank(org.getIndustryId())) {
            organizationDTO.setIndustryId(org.getIndustryId());
        }
        if (StringUtils.isNotBlank(org.getFormalPackageId())) {
            organizationDTO.setFormalPackageId(org.getFormalPackageId());
        }
        if (org.hasFormalExpireTime()) {
            organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(org.getFormalExpireTime()));
        }
        if (org.hasStatus()) {
            organizationDTO.setStatus(org.getStatusValue());
        }
        if (org.hasSource()) {
            organizationDTO.setSource(org.getSourceValue());
        }
        if (org.hasRootGroupId()) {
            organizationDTO.setRootGroupId(org.getRootGroupId());
        }
        if (org.hasPackageId()) {
            organizationDTO.setPackageId(org.getPackageId());
        }
        if (org.hasTrialPackageId()) {
            organizationDTO.setTrialPackageId(org.getTrialPackageId());
        }
        if (org.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(org.getTrialExpireTime()));
        }
        if (org.hasAdminPhone()) {
            organizationDTO.setAdminPhone(org.getAdminPhone());
        }
        if (org.hasPackageExpireTime()) {
            organizationDTO.setPackageExpireTime(GrpcDateUtil.getDate(org.getPackageExpireTime()));
        }
        if (org.hasFreeExpireTime()) {
            organizationDTO.setFreeExpireTime(GrpcDateUtil.getDate(org.getFreeExpireTime()));
        }
        if (org.hasPriId()) {
            organizationDTO.setPrivateKeyId(org.getPriId());
        }
        if (org.hasDeliverAudit()) {
            organizationDTO.setDeliverCheckFlag(BooleanStatusEnum.toInteger(org.getDeliverAudit()));
        }
        if (org.hasComplete()) {
            organizationDTO.setCompleteFlag(BooleanStatusEnum.toInteger(org.getComplete()));
        }
        if (org.hasAdminName()) {
            organizationDTO.setAdminName(org.getAdminName());
        }
        if (org.hasWalletId()) {
            organizationDTO.setCorpWallet(org.getWalletId());
        }
        if (org.hasSmsPrice()) {
            organizationDTO.setSmsPrice((short) org.getSmsPrice());
        }
        if (org.hasFooter()) {
            organizationDTO.setFooter(org.getFooterValue());
        }
        if (org.hasAuditSmtpId()) {
            organizationDTO.setCheckSmtpId(org.getAuditSmtpId());
        }
        if (org.hasNotifySales()) {
            organizationDTO.setNotifySalesFlag(BooleanStatusEnum.toInteger(org.getNotifySales()));
        }
        if (org.hasInner()) {
            organizationDTO.setInnerFlag(BooleanStatusEnum.toInteger(org.getInner()));
        }
        if (org.hasDeliveryAuth()) {
            organizationDTO.setDeliveryAuthType(org.getDeliveryAuthValue());
        }
        if (org.hasPaasConf()) {
            organizationDTO.setPaasConf(org.getPaasConf());
        }
        if (org.hasDistribution()) {
            organizationDTO.setDistribution(org.getDistribution());
        }
        if (CollectionUtils.isNotEmpty(org.getContractUrlsList())) {
            organizationDTO.setContractUrls(org.getContractUrls(0));
        }
        if (org.hasSkin()) {
            organizationDTO.setSkin(org.getSkin());
        }
        if (org.hasSourceRemark()) {
            organizationDTO.setSourceRemark(org.getSourceRemark());
        }
        if (org.hasPubFilePath()) {
            organizationDTO.setPublicKeyFilePath(org.getPubFilePath());
        }
        if (org.hasBanLoginTrialExpiration()) {
            organizationDTO.setBanLoginTrialExpiration(BooleanStatusEnum.toInteger(org.getBanLoginTrialExpiration()));
        }
        if (org.hasLocation()) {
            organizationDTO.setLocation(org.getLocation());
        }
        if (org.hasCrmStatus()) {
            organizationDTO.setCrmStatus(org.getCrmStatus());
        }
        if (org.hasSeoSource()) {
            organizationDTO.setSeoSource(org.getSeoSource());
        }
        if (org.hasSensitiveWordEnable()) {
            organizationDTO.setSensitiveWordEnable(BooleanStatusEnum.toInteger(org.getSensitiveWordEnable()));
        }
        return organizationDTO;
    }

    /**
     * 公司操作信息转换成公司信息DTO对象
     *
     * @param org 公司操作信息
     * @return com.bestcem.xm.user.service.dto.user.OrganizationDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/30 10:40
     */
    public static OrganizationDTO createOrgToOrganizationDTO(CreateOrg org) {
        OrganizationDTO organizationDTO = new OrganizationDTO();

        if (StringUtils.isNotBlank(org.getCode())) {
            organizationDTO.setCode(org.getCode());
        }
        if (StringUtils.isNotBlank(org.getName())) {
            organizationDTO.setName(org.getName());
        }
        if (StringUtils.isNotBlank(org.getAdminEmail())) {
            organizationDTO.setAdminEmail(org.getAdminEmail());
        }
        if (StringUtils.isNotBlank(org.getIndustryId())) {
            organizationDTO.setIndustryId(org.getIndustryId());
        }
        if (StringUtils.isNotBlank(org.getFormalPackageId())) {
            organizationDTO.setFormalPackageId(org.getFormalPackageId());
        }
        if (org.hasFormalExpireTime()) {
            organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(org.getFormalExpireTime()));
        }
        if (org.hasStatus()) {
            organizationDTO.setStatus(org.getStatusValue());
        }
        if (org.hasSource()) {
            organizationDTO.setSource(org.getSourceValue());
        }
        if (org.hasRootGroupId()) {
            organizationDTO.setRootGroupId(org.getRootGroupId());
        }
        if (org.hasPackageId()) {
            organizationDTO.setPackageId(org.getPackageId());
        }
        if (org.hasTrialPackageId()) {
            organizationDTO.setTrialPackageId(org.getTrialPackageId());
        }
        if (org.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(org.getTrialExpireTime()));
        }
        if (org.hasAdminPhone()) {
            organizationDTO.setAdminPhone(org.getAdminPhone());
        }
        if (org.hasPackageExpireTime()) {
            organizationDTO.setPackageExpireTime(GrpcDateUtil.getDate(org.getPackageExpireTime()));
        }
        if (org.hasFreeExpireTime()) {
            organizationDTO.setFreeExpireTime(GrpcDateUtil.getDate(org.getFreeExpireTime()));
        }
        if (org.hasPriId()) {
            organizationDTO.setPrivateKeyId(org.getPriId());
        }
        if (org.hasDeliverAudit()) {
            organizationDTO.setDeliverCheckFlag(BooleanStatusEnum.toInteger(org.getDeliverAudit()));
        }
        if (org.hasComplete()) {
            organizationDTO.setCompleteFlag(BooleanStatusEnum.toInteger(org.getComplete()));
        }
        if (org.hasAdminName()) {
            organizationDTO.setAdminName(org.getAdminName());
        }
        if (org.hasWalletId()) {
            organizationDTO.setCorpWallet(org.getWalletId());
        }
        if (org.hasSmsPrice()) {
            organizationDTO.setSmsPrice((short) org.getSmsPrice());
        }
        if (org.hasFooter()) {
            organizationDTO.setFooter(org.getFooterValue());
        }
        if (org.hasAuditSmtpId()) {
            organizationDTO.setCheckSmtpId(org.getAuditSmtpId());
        }
        if (org.hasNotifySales()) {
            organizationDTO.setNotifySalesFlag(BooleanStatusEnum.toInteger(org.getNotifySales()));
        }
        if (org.hasInner()) {
            organizationDTO.setInnerFlag(BooleanStatusEnum.toInteger(org.getInner()));
        }
        if (org.hasDeliveryAuth()) {
            organizationDTO.setDeliveryAuthType(org.getDeliveryAuthValue());
        }
        if (org.hasPaasConf()) {
            organizationDTO.setPaasConf(org.getPaasConf());
        }
        if (org.hasDistribution()) {
            organizationDTO.setDistribution(org.getDistribution());
        }
        if (CollectionUtils.isNotEmpty(org.getContractUrlsList())) {
            organizationDTO.setContractUrls(org.getContractUrls(0));
        }
        if (org.hasSkin()) {
            organizationDTO.setSkin(org.getSkin());
        }
        if (org.hasSourceRemark()) {
            organizationDTO.setSourceRemark(org.getSourceRemark());
        }
        if (org.hasPubFilePath()) {
            organizationDTO.setPublicKeyFilePath(org.getPubFilePath());
        }
        return organizationDTO;
    }

    /**
     * 系统商后台新增机构信息转换成公司信息DTO对象
     *
     * @param request
     * @return com.bestcem.xm.user.service.dto.user.OrganizationDTO
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/7/6 10:42
     */
    public static OrganizationDTO createRequestToOrganizationDTO(AcceptBookingRequest request) {
        OrganizationDTO organizationDTO = new OrganizationDTO();

        organizationDTO.setName(request.getOrgName());
        organizationDTO.setIndustryId(request.getIndustryId());
        organizationDTO.setAdminPhone(request.getAdminPhone());
        organizationDTO.setPackageId(request.getPackageId());
        organizationDTO.setPackageExpireTime(GrpcDateUtil.getDate(request.getPackageExpireTime()));
        organizationDTO.setFormalPackageId(request.getFormalPackageId());
        organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(request.getFormalExpireTime()));
        organizationDTO.setSmsPrice((short) request.getSmsPrice());
        if (request.hasOrgCode()) {
            organizationDTO.setCode(request.getOrgCode());
        }
        if (request.hasAdminEmail()) {
            organizationDTO.setAdminEmail(request.getAdminEmail());
        }
        if (request.hasTrialPackageId()) {
            organizationDTO.setTrialPackageId(request.getTrialPackageId());
        }
        if (request.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(request.getTrialExpireTime()));
        }
        if (request.hasDistribution()) {
            organizationDTO.setDistribution(request.getDistribution());
        }
        if (CollectionUtils.isNotEmpty(request.getContractUrlsList())) {
            organizationDTO.setContractUrls(JSON.toJSONString(request.getContractUrlsList()));
        }
        if (request.hasSource()) {
            organizationDTO.setSource(request.getSourceValue());
        }
        if (request.hasLocation()) {
            organizationDTO.setLocation(request.getLocation());
        }
        if (request.hasCrmStatus()) {
            organizationDTO.setCrmStatus(request.getCrmStatus());
        }
        if (request.hasSeoSource()) {
            organizationDTO.setSeoSource(request.getSeoSource());
        }
        if (request.hasSensitiveWordEnable()) {
            organizationDTO.setSensitiveWordEnable(BooleanStatusEnum.toInteger(request.getSensitiveWordEnable()));
        }
        return organizationDTO;
    }

    /**
     * 系统商后台新增机构信息转换成公司信息DTO对象
     *
     * @param request
     * @return com.bestcem.xm.user.service.dto.user.OrganizationDTO
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/7/6 10:42
     */
    public static OrganizationDTO createRequestToOrganizationDTO(CreateOrgFromBackendRequest request) {
        OrganizationDTO organizationDTO = new OrganizationDTO();

        organizationDTO.setName(request.getOrgName());
        organizationDTO.setIndustryId(request.getIndustryId());
        organizationDTO.setAdminPhone(request.getAdminPhone());
        organizationDTO.setAdminEmail(request.getAdminEmail());
        organizationDTO.setFormalPackageId(request.getFormalPackageId());
        organizationDTO.setFormalExpireTime(GrpcDateUtil.getDate(request.getFormalExpireTime()));
        organizationDTO.setSmsPrice((short) request.getSmsPrice());
        organizationDTO.setFooter(request.getFooterValue());
        organizationDTO.setBanLoginTrialExpiration(BooleanStatusEnum.toInteger(request.getBanLoginTrialExpiration()));
        organizationDTO.setInnerFlag(BooleanStatusEnum.toInteger(request.getInner()));
        organizationDTO.setDeliveryAuthType(request.getDeliveryAuthValue());
        organizationDTO.setCheckExpiry(request.getCheckExpiry());
        organizationDTO.setStatus(request.getStatusValue());
        organizationDTO.setSensitiveWordEnable(BooleanStatusEnum.toInteger(request.getSensitiveWordEnable()));
        if (request.hasOrgCode()) {
            organizationDTO.setCode(request.getOrgCode());
        }
        if (request.hasOrgParentCode()) {
            organizationDTO.setParentCode(request.getOrgParentCode());
        }
        if (request.hasSource()) {
            organizationDTO.setSource(request.getSourceValue());
        }
        if (request.hasSourceRemark()) {
            organizationDTO.setSourceRemark(request.getSourceRemark());
        }
        if (request.hasTrialPackageId()) {
            organizationDTO.setTrialPackageId(request.getTrialPackageId());
        }
        if (request.hasTrialExpireTime()) {
            organizationDTO.setTrialExpireTime(GrpcDateUtil.getDate(request.getTrialExpireTime()));
        }
        if (CollectionUtils.isNotEmpty(request.getContractUrlsList())) {
            organizationDTO.setContractUrls(request.getContractUrlsList().get(0));
        }
        if (request.hasLocation()) {
            organizationDTO.setLocation(request.getLocation());
        }
        return organizationDTO;
    }

    public static List<SampleCode> orgDtos2SampleCodes(List<OrganizationDTO> orgs) {
        if (orgs == null) {
            return Collections.emptyList();
        }

        List<SampleCode> list = new ArrayList<>(orgs.size());
        for (OrganizationDTO organizationDTO : orgs) {
            list.add(organizationDTOToSampleCode(organizationDTO));
        }

        return list;
    }

    public static SampleCode organizationDTOToSampleCode(OrganizationDTO organizationDTO) {
        if (organizationDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.SampleCode.Builder sampleCode = SampleCode.newBuilder();

        if (organizationDTO.getOrgId() != null) {
            sampleCode.setOrgId(organizationDTO.getOrgId());
        }

        if (organizationDTO.getCode() != null) {
            sampleCode.setOrgCode(organizationDTO.getCode());
        }

        return sampleCode.build();
    }


    /**
     * 公司DTO to 公司msg
     *
     * @param organizationDTO 公司DTO
     * @return 公司msg
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 16:01 2022/6/30
     */
    public static Organization dto2Msg(OrganizationDTO organizationDTO) {
        Organization.Builder builder = Organization.newBuilder();
        String orgId = organizationDTO.getOrgId();
        String code = organizationDTO.getCode();
        String name = organizationDTO.getName();
        String content = organizationDTO.getContent();
        Integer status = organizationDTO.getStatus();
        Integer source = organizationDTO.getSource();
        String sourceRemark = organizationDTO.getSourceRemark();
        String rootGroupId = organizationDTO.getRootGroupId();
        String industryId = organizationDTO.getIndustryId();
        String packageId = organizationDTO.getPackageId();
        String formalPackageId = organizationDTO.getFormalPackageId();
        Date formalExpireTime = organizationDTO.getFormalExpireTime();
        String trialPackageId = organizationDTO.getTrialPackageId();
        Date trialExpireTime = organizationDTO.getTrialExpireTime();
        String adminEmail = organizationDTO.getAdminEmail();
        String adminPhone = organizationDTO.getAdminPhone();
        Date packageExpireTime = organizationDTO.getPackageExpireTime();
        Date freeExpireTime = organizationDTO.getFreeExpireTime();
        String skin = organizationDTO.getSkin();
        String publicKeyFilePath = organizationDTO.getPublicKeyFilePath();
        String privateKeyId = organizationDTO.getPrivateKeyId();
        Integer deliverCheckFlag = organizationDTO.getDeliverCheckFlag();
        Integer completeFlag = organizationDTO.getCompleteFlag();
        String adminName = organizationDTO.getAdminName();
        String corpWallet = organizationDTO.getCorpWallet();
        Short smsPrice = organizationDTO.getSmsPrice();
        Integer footer = organizationDTO.getFooter();
        String checkSmtpId = organizationDTO.getCheckSmtpId();
        Integer notifySalesFlag = organizationDTO.getNotifySalesFlag();
        Integer innerFlag = organizationDTO.getInnerFlag();
        String paasConf = organizationDTO.getPaasConf();
        String distribution = organizationDTO.getDistribution();
        Integer deliveryAuthType = organizationDTO.getDeliveryAuthType();
        String contractUrls = organizationDTO.getContractUrls();
        String parentId = organizationDTO.getParentId();
        Integer sensitiveWordEnable = organizationDTO.getSensitiveWordEnable();

        if (Objects.nonNull(orgId)) {
            builder.setId(orgId);
        }
        if (Objects.nonNull(code)) {
            builder.setCode(code);
        }
        if (Objects.nonNull(name)) {
            builder.setName(name);
        }
        if (Objects.nonNull(content)) {
            builder.setContent(content);
        }
        if (Objects.nonNull(status)) {
            builder.setStatusValue(status);
        }
        if (Objects.nonNull(source)) {
            builder.setSourceValue(source);
        }
        if (Objects.nonNull(sourceRemark)) {
            builder.setSourceRemark(sourceRemark);
        }
        if (Objects.nonNull(rootGroupId)) {
            builder.setRootGroupId(rootGroupId);
        }
        if (Objects.nonNull(industryId)) {
            builder.setIndustryId(industryId);
        }
        if (Objects.nonNull(packageId)) {
            builder.setPackageId(packageId);
        }
        if (Objects.nonNull(formalPackageId)) {
            builder.setFormalPackageId(formalPackageId);
        }
        if (Objects.nonNull(formalExpireTime)) {
            builder.setFormalExpireTime(GrpcDateUtil.getTimestamp(formalExpireTime));
        }
        if (Objects.nonNull(trialPackageId)) {
            builder.setTrialPackageId(trialPackageId);
        }
        if (Objects.nonNull(trialExpireTime)) {
            builder.setTrialExpireTime(GrpcDateUtil.getTimestamp(trialExpireTime));
        }
        if (Objects.nonNull(adminEmail)) {
            builder.setAdminEmail(adminEmail);
        }
        if (Objects.nonNull(adminPhone)) {
            builder.setAdminPhone(adminPhone);
        }
        if (Objects.nonNull(packageExpireTime)) {
            builder.setPackageExpireTime(GrpcDateUtil.getTimestamp(packageExpireTime));
        }
        if (Objects.nonNull(freeExpireTime)) {
            builder.setFreeExpireTime(GrpcDateUtil.getTimestamp(freeExpireTime));
        }
        if (Objects.nonNull(skin)) {
            builder.setSkin(skin);
        }
        if (Objects.nonNull(publicKeyFilePath)) {
            builder.setPubFilePath(publicKeyFilePath);
        }
        if (Objects.nonNull(privateKeyId)) {
            builder.setPriId(privateKeyId);
        }
        if (Objects.nonNull(deliverCheckFlag)) {
            builder.setDeliverAudit(deliverCheckFlag.equals(1));
        }
        if (Objects.nonNull(completeFlag)) {
            builder.setComplete(completeFlag.equals(1));
        }
        if (Objects.nonNull(adminName)) {
            builder.setAdminName(adminName);
        }
        if (Objects.nonNull(corpWallet)) {
            builder.setWalletId(corpWallet);
        }
        if (Objects.nonNull(smsPrice)) {
            builder.setSmsPrice(smsPrice);
        }
        if (Objects.nonNull(footer)) {
            builder.setFooter(com.bestcem.bp.xm.user.grpc.v1.enums.OrgFooterEnum.OrgFooter.forNumber(footer));
        }
        if (Objects.nonNull(checkSmtpId)) {
            builder.setAuditSmtpId(checkSmtpId);
        }
        if (Objects.nonNull(notifySalesFlag)) {
            builder.setNotifySales(notifySalesFlag.equals(1));
        }
        if (Objects.nonNull(innerFlag)) {
            builder.setInner(innerFlag.equals(1));
        }
        if (Objects.nonNull(paasConf)) {
            builder.setPaasConf(paasConf);
        }
        if (Objects.nonNull(distribution)) {
            builder.setDistribution(distribution);
        }
        if (Objects.nonNull(deliveryAuthType)) {
            builder.setDeliveryAuthValue(deliveryAuthType);
        }
        if (Objects.nonNull(contractUrls)) {
            builder.addAllContractUrls(JSON.parseArray(contractUrls, String.class));
        }
        if (Objects.nonNull(parentId)) {
            builder.setParentId(parentId);
        }
        if (Objects.nonNull(sensitiveWordEnable)) {
            builder.setSensitiveWordEnable(sensitiveWordEnable.equals(1));
        }
        return builder.build();
    }


    public static ListOrgsByFilterParam listOrgsByFilterRequest2Param(ListOrgsByFilterRequest request) {
        ListOrgsByFilterParam param = new ListOrgsByFilterParam();
        PageParam pageParam = new PageParam(request.getPage().getPage(), request.getPage().getSize());
        param.setPageParam(pageParam);
        param.setStatusList(request.getStatusesValueList());
        param.setCodeList(request.getCodesList());
        param.setSourceList(request.getSourcesValueList());
        param.setSourceRemarkList(request.getSourceRemarksList());
        return param;
    }
}
